package com.cn.dao;

import com.cn.bean.User;
import com.cn.util.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * 用户Dao
 */
public class UserDao {

	// 依据用户名和密码查询用户
	public User query(String username, String password)  {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		User user = null;
		try {
			String sql = "select * from goods_user where userName=? and userPassword=?";
			connection = JDBCUtils.getConnection();
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, username);
			preparedStatement.setString(2, password);
			resultSet = preparedStatement.executeQuery();
			if (resultSet.next()) {
				user = new User();
				user.setUserID(resultSet.getInt("userID"));
				user.setUserName(resultSet.getString("userName"));
				user.setUserPassword(resultSet.getString("userPassword"));
			}
		} catch (Exception e) {
			System.out.println(e.toString());
		} finally {
			JDBCUtils.release(connection, preparedStatement, resultSet);
		}
		return user;
	}
}